#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mutex;
std::condition_variable con;

//一个打印奇数 一个打印偶数
int main()
{
	bool flag=true;
	int n = 10;
	std::thread t1([&](){//打印奇数
		int i=1;
		while(i<n)
		{
			std::unique_lock<std::mutex>lock(mutex);
			con.wait(lock,[&]()->bool{
				return !flag;
			});
			std::cout<<"t1:"<<i<<std::endl;
			i+=2;
			flag=true;
			con.notify_one();
		}
	});
	std::thread t2([&](){//打印偶数
		int j=0;//打印偶数
		while(j<n)
		{
			std::unique_lock<std::mutex>lock(mutex);
			con.wait(lock,[&]()->bool{
				return flag;
			});
			std::cout<<"t2:"<<j<<std::endl;
			j+=2;
			flag=false;
			con.notify_one();
		}
	});
	t1.join();
	t2.join();
	return 0;
}